<template>
  <ContentWrap>
    <div class="content"
      ><el-icon color="#1890ff"><InfoFilled /></el-icon>上次更新时间：{{ time }}
      <strong></strong>
      <span @click="getApi">立即更新</span></div
    >
    <el-table :data="textInfo" style="width: 100%" v-loading="loading">
      <el-table-column label="参数">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-tag
              class="ml-2"
              type="info"
              style="color: #fff"
              :color="scope.row.color"
              effect="light"
              >Tag {{ scope.row.name }}</el-tag
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="描述">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <span>{{ scope.row.text }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="当前值">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <span>{{ scope.row.unit }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'

import { ContentWrap } from '@/components/ContentWrap'
import { fillZero, timestamp } from './common'

import {
  getSystemByToken,
  getJvmMemoryMax,
  getJvmMemoryCommitted,
  getJvmMemoryUsed,
  getJvmBufferMemoryUsed,
  getJvmBufferCount,
  getJvmThreadsDaemon,
  getJvmThreadsLive,
  getJvmThreadsPeak,
  getJvmClassesLoaded,
  getJvmClassesUnloaded,
  getJvmGcMemoryAllocated,
  getJvmGcMemoryPromoted,
  getJvmGcMaxDataSize,
  getJvmGcLiveDataSize,
  getJvmGcPause
} from '@/api/jvmInfo/index'
const loading = ref(true)
const time = ref('2023年4月7日 11时38分50秒')
const textInfo = ref([
  { name: 'jvm.memory.max', color: '#d3adf7', text: 'JVM 最大内存', unit: '' },
  { name: 'jvm.memory.committed', color: '#d3adf7', text: 'JVM 可用内存', unit: '' },
  { name: 'jvm.memory.used', color: '#d3adf7', text: 'JVM 已用内存', unit: '' },
  { name: 'jvm.buffer.memory.used', color: '#87e8de', text: 'JVM 缓冲区已用内存', unit: '' },
  { name: 'jvm.buffer.count', color: '#87e8de', text: '当前缓冲区数量', unit: '' },
  { name: 'jvm.threads.daemon', color: '#b7eb8f', text: 'JVM 守护线程数量', unit: '' },
  { name: 'jvm.threads.live', color: '#b7eb8f', text: 'JVM 当前活跃线程数量', unit: '' },
  { name: 'jvm.threads.peak', color: '#b7eb8f', text: 'JVM 峰值线程数量', unit: '' },
  { name: 'jvm.classes.loaded', color: '#ffd591', text: 'JVM 已加载 Class 数量', unit: '' },
  { name: 'jvm.classes.unloaded', color: '#ffd591', text: 'JVM 未加载 Class 数量', unit: '' },
  {
    name: 'jvm.gc.memory.allocated',
    color: '#ffadd2',
    text: 'GC 时, 年轻代分配的内存空间',
    unit: ''
  },
  {
    name: 'jvm.gc.memory.promoted',
    color: '#ffadd2',
    text: 'GC 时, 老年代分配的内存空间',
    unit: ''
  },
  { name: 'jvm.gc.max.data.size', color: '#ffadd2', text: 'GC 时, 老年代的最大内存空间', unit: '' },
  {
    name: 'jvm.gc.live.data.size',
    color: '#ffadd2',
    text: 'FullGC 时, 老年代的内存空间',
    unit: ''
  },
  { name: 'jvm.gc.pause.count', color: '#91d5ff', text: '系统启动以来GC 次数', unit: '' },
  { name: 'jvm.gc.pause.totalTime', color: '#91d5ff', text: '系统启动以来GC 总耗时', unit: '' }
])

const getApi = async () => {
  let resultJvmMemoryMax = await getJvmMemoryMax({})
  let resultByToken = await getSystemByToken({})
  let resultJvmMemoryCommitted = await getJvmMemoryCommitted({})
  let resultJvmMemoryUsed = await getJvmMemoryUsed({})
  let resultJvmBufferMemoryUsed = await getJvmBufferMemoryUsed({})
  let resultJvmBufferCount = await getJvmBufferCount({})
  let resultJvmThreadsDaemon = await getJvmThreadsDaemon({})
  let resultJvmThreadsLive = await getJvmThreadsLive({})
  let resultJvmThreadsPeak = await getJvmThreadsPeak({})
  let resultJvmClassesLoaded = await getJvmClassesLoaded({})
  let resultJvmClassesUnloaded = await getJvmClassesUnloaded({})
  let resultJvmGcMemoryAllocated = await getJvmGcMemoryAllocated({})
  let resultJvmGcMemoryPromoted = await getJvmGcMemoryPromoted({})
  let resultJvmGcMaxDataSize = await getJvmGcMaxDataSize({})
  let resultJvmGcLiveDataSize = await getJvmGcLiveDataSize({})
  let resultJvmGcPause = await getJvmGcPause({})
  let timeDate = new Date(resultByToken.timestamp)
  time.value = `${timeDate.getFullYear()}年${timeDate.getMonth() + 1}月${timeDate.getDate()}日
     ${fillZero(timeDate.getHours())}时${fillZero(timeDate.getMinutes())}分${fillZero(
    timeDate.getSeconds()
  )}秒`

  textInfo.value.map((item) => {
    if (item.name.indexOf(resultJvmGcPause.name) > -1) {
      if (item.name.indexOf('count') > -1) {
        item.unit = Number(resultJvmGcPause.measurements[0].value) + ' 次'
      } else {
        item.unit = Number(resultJvmGcPause.measurements[1].value) + ' 秒'
      }
    }
    switch (item.name) {
      case resultJvmMemoryMax.name:
        item.unit =
          (Number(resultJvmMemoryMax.measurements[0].value) / 1024 / 1024).toFixed(3) + ' MB'
        break
      case resultJvmMemoryCommitted.name:
        item.unit =
          (Number(resultJvmMemoryCommitted.measurements[0].value) / 1024 / 1024).toFixed(3) + ' MB'
        break
      case resultJvmMemoryUsed.name:
        item.unit =
          (Number(resultJvmMemoryUsed.measurements[0].value) / 1024 / 1024).toFixed(3) + ' MB'
        break
      case resultJvmMemoryUsed.name:
        item.unit =
          (Number(resultJvmMemoryUsed.measurements[0].value) / 1024 / 1024).toFixed(3) + ' MB'
        break
      case resultJvmBufferMemoryUsed.name:
        item.unit =
          (Number(resultJvmBufferMemoryUsed.measurements[0].value) / 1024 / 1024).toFixed(3) + ' MB'
        break
      case resultJvmBufferCount.name:
        item.unit = Number(resultJvmBufferCount.measurements[0].value) + ' 个'
        break
      case resultJvmThreadsDaemon.name:
        item.unit = Number(resultJvmThreadsDaemon.measurements[0].value) + ' 个'
        break
      case resultJvmThreadsLive.name:
        item.unit = Number(resultJvmThreadsLive.measurements[0].value) + ' 个'
        break
      case resultJvmThreadsPeak.name:
        item.unit = Number(resultJvmThreadsPeak.measurements[0].value) + ' 个'
        break
      case resultJvmClassesLoaded.name:
        item.unit = Number(resultJvmClassesLoaded.measurements[0].value) + ' 个'
        break
      case resultJvmClassesUnloaded.name:
        item.unit = Number(resultJvmClassesUnloaded.measurements[0].value) + ' 个'
        break
      case resultJvmGcMemoryAllocated.name:
        item.unit =
          Number(resultJvmGcMemoryAllocated.measurements[0].value / 1024 / 1024).toFixed(3) + ' MB'
        break
      case resultJvmGcMemoryPromoted.name:
        item.unit =
          Number(resultJvmGcMemoryPromoted.measurements[0].value / 1024 / 1024).toFixed(3) + ' MB'
        break
      case resultJvmGcMaxDataSize.name:
        item.unit =
          Number(resultJvmGcMaxDataSize.measurements[0].value / 1024 / 1024).toFixed(3) + ' MB'
        break
      case resultJvmGcLiveDataSize.name:
        item.unit =
          Number(resultJvmGcLiveDataSize.measurements[0].value / 1024 / 1024).toFixed(3) + ' MB'
        break
      default:
        console.log(item.name) // 如果没有与表达式相同的值，则执行该代码
    }
    loading.value = false
  })
}
onMounted(() => {
  getApi()
})
</script>

<style lang="less" scoped>
.content {
  display: flex;
  align-items: center;
  background-color: #e6f7ff;
  border: 1px solid #91d5ff;
  padding: 8px 15px 8px 17px;
  font-size: 14px;
  margin-bottom: 20px;
  i {
    margin-right: 4px;
  }
  strong {
    width: 1px;
    border-left: 1px solid #ccc;
    height: 16px;
    margin: 0 10px;
    display: block;
  }
  span {
    color: #1890ff;
    cursor: pointer;
  }
}
</style>
